package logic

import (
	"context"

	"server_1.0/service/question/internal/svc"
	"server_1.0/service/question/model"
	"server_1.0/service/question/question"
	"server_1.0/util"

	"github.com/zeromicro/go-zero/core/logx"
)

type InsertLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInsertLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InsertLogic {
	return &InsertLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *InsertLogic) Insert(in *question.InsertReq) (*question.QReps, error) {
	code := util.Success
	newQus := model.Question{
		Qno:   in.No,
		Qinfo: in.Info,
		Qopa:  in.OPa,
		Qopb:  in.OPb,
		Qawa:  in.AWa,
		Qawb:  in.AWb,
	}
	usersHandle := make([]*question.Reps, 0)
	count := 0
	_, err := l.svcCtx.QuestionModel.Insert(l.ctx, &newQus)
	if err != nil {
		code = util.AddError
	}

	usersHandle = append(
		usersHandle,
		&question.Reps{
			Code: int64(code),
			No:   newQus.Qno,
			Info: newQus.Qinfo,
			OPa:  newQus.Qopa,
			OPb:  newQus.Qopb,
			AWa:  newQus.Qawa,
			AWb:  newQus.Qawb,
		})
	count += 1
	return &question.QReps{
		Count: int64(count),
		QRs:   usersHandle,
	}, nil
}
